package com.ruoyi.asset.tool.controller;

import cn.dev33.satoken.annotation.SaIgnore;

import com.ruoyi.asset.tool.domain.bo.ToolSearchBo;
import com.ruoyi.asset.tool.domain.vo.ToolVo;
import com.ruoyi.asset.tool.service.IToolService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 作业工具
 *
 * @author lq
 * @date 2023-11-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/tool")
public class ToolController extends BaseController {

	private final IToolService toolService;

	/**
	 * 导入作业工具数据
	 */
    //@SaCheckPermission("standingbook:cleaningImportBase:add")
	@SaIgnore()
    //@Log(title = "作业工具导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping(value = "/importData")
    public R<List<String>> importData(@RequestParam String excelUrl, @RequestParam String serviceType) {
        List<String> list = this.toolService.importExcel(excelUrl, serviceType);

        if (list == null) {
            return R.fail();
        } else {
            return R.ok(list);
        }
    }

	/**
	 * 导出作业工具模板
	 */
	@SaIgnore()
	@PostMapping("/importTemplate")
	public void exportExcel(HttpServletResponse response) {
		ExcelUtil.exportExcel(new ArrayList<>(), "作业工具导入模板", ToolVo.class, response);
	}

    /**
     * 删除作业工具数据
     */
    @SaIgnore()
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                              @PathVariable Long[] ids) {
        return toAjax(toolService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询作业工具信息
     */
    @SaIgnore()
    @GetMapping("/list")
    public TableDataInfo<ToolVo> list(ToolSearchBo searchBo, PageQuery pageQuery) {
        return this.toolService.queryPageList(searchBo, pageQuery);
    }

    /**
     * 导出作业工具
     */
    @SaIgnore()
    @PostMapping("/export")
    //public void export(HttpServletResponse response, @RequestBody(required = false) ToolSearchBo searchBo) {
    public void export(ToolSearchBo searchBo, HttpServletResponse response) {
        List<ToolVo> list = this.toolService.exportTool(searchBo);

        ExcelUtil.exportExcel(list, "作业工具", ToolVo.class, response);
    }
}
